package com.google.screen;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class Authentication extends AsyncTask<String, Integer, ContentValues> {
	
	private Context mCtx;
	
	public Authentication(Context ctx) {
		mCtx = ctx;
	}
	
	@Override
	protected ContentValues doInBackground(String... params) {
		ContentValues data = new ContentValues();
		HttpClient httpclient = new DefaultHttpClient();
		org.apache.http.HttpResponse response;
		String responseString = null;
		try {
			response = httpclient.execute(new HttpGet(params[0]));
			StatusLine statusLine = response.getStatusLine();
			Log.i("status respone", statusLine.toString());

			if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				response.getEntity().writeTo(out);
				out.close();
				responseString = out.toString();
				Log.i("JSON", responseString);

				JSONObject jso = new JSONObject(responseString);
				JSONObject jHeader = jso.getJSONObject("header");
				
				data.put("statusheader", jHeader.getString("status"));
				data.put("code", jHeader.getString("code"));
				data.put("message", jHeader.getString("message"));
				
				if (params[1].equals("login") && 
						data.getAsString("statusheader").equals("ok") && 
						data.getAsInteger("code") == 1) {
					JSONObject jData = jso.getJSONObject("data");
					data.put("token", jData.getString("token"));
				}
				
				if ((data.getAsInteger("code") == -3 ||
						data.getAsInteger("code") == 1) && 
							params[1].equals("checkout")) {
					JSONObject jData = jso.getJSONObject("data");
					data.put("coupon_id", jData.getString("coupon_id"));
					data.put("category_id", jData.getString("category_id"));
					data.put("title", jData.getString("title"));
					data.put("description", jData.getString("description"));
					data.put("image", jData.getString("image"));
					data.put("nbr_to_print", jData.getString("nbr_to_print"));
					data.put("nbr_printed", jData.getString("nbr_printed"));
					data.put("start_date", jData.getString("start_date"));
					data.put("end_date", jData.getString("end_date"));
					data.put("status", jData.getString("status"));
					data.put("checkout", jData.getString("checkout"));
				}
				return data;
			} else {
				response.getEntity().getContent().close();
				Log.i("status respone", statusLine.getReasonPhrase());
			}
		} catch (ClientProtocolException e) {
			Log.i("ClientProtocolException", e.getMessage());
		} catch (IOException e) {
			Log.i("IOException", e.getMessage());
		} catch (JSONException e) {
			Log.i("JSONException", e.getMessage());
		}
		return null;
	}

}
